package com.om.opensourway;

import org.apache.spark.util.AccumulatorV2;

import java.util.HashMap;

public class MyAccumulator extends AccumulatorV2<HashMap<String, Long>, HashMap<String, Long>> {
    private HashMap<String, Long> map = new HashMap<String, Long>();

    @Override
    public boolean isZero() {
        return map.isEmpty();
    }

    @Override
    public AccumulatorV2<HashMap<String, Long>, HashMap<String, Long>> copy() {
        MyAccumulator newMyAccu = new MyAccumulator();
        map.putAll(map);
        return newMyAccu;
    }

    @Override
    public void reset() {
        map = new HashMap<String, Long>();
    }

    @Override
    public void add(HashMap<String, Long> v) {
        map.putAll(v);
    }

    @Override
    public void merge(AccumulatorV2<HashMap<String, Long>, HashMap<String, Long>> other) {
        map.putAll(other.value());
    }

    @Override
    public HashMap<String, Long> value() {
        return map;
    }
}
